<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<el-form :model="addForm" :rules="rules" ref="addForm" class="demo-ruleForm">
				<el-row :gutter="40">
					<el-col :span="8">
						<el-form-item class="formList" prop="name" label="客户：">
							<el-input clearable class="input_right" v-model="addForm.customer"
									  :disabled="type == 'see'"></el-input>
							<span @click="copyValue(addForm.customer)"  style="color: #1890ff;cursor: pointer;position: absolute;right: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
						</el-form-item>
						<el-form-item class="formList" prop="orderId" label="订单号：">
							<el-input clearable class="input_right" v-model="addForm.orderNum" disabled></el-input>
							<span @click="copyValue(addForm.orderNum)"  style="color: #1890ff;cursor: pointer;position: absolute;right: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="formList" prop="type" label="订单类型：">
							<el-select clearable class="input_right" v-model="addForm.orderType" :disabled="type == 'see'">
								<el-option label="内销" value="内销"></el-option>
								<el-option label="外置" value="外置"></el-option>
							</el-select>
						</el-form-item>
						<el-form-item class="formList" prop="state" label="订单状态：">
							<el-input clearable class="input_right" v-model="addForm.orderStatusCn" disabled></el-input>

						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="formList" prop="person" label="下单人：">
							<el-input clearable class="input_right" v-model="addForm.createUserName+' '+addForm.createTimeFt" disabled></el-input>
						</el-form-item>
						<el-form-item class="formList" prop="person" label="确认人：">
							<el-input clearable class="input_right" v-model="addForm.sureUserName +''+addForm.sureTime"
									  disabled></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<div class="tableList">
					<table border="1">
						<tr>
							<th>品类</th>
							<th width="200">产品名称</th>
							<th width="200">规格</th>
							<th>单件万支</th>
							<th>件数</th>
							<th>合计(万支)</th>
<!--							<th>单价</th>-->
<!--							<th>金额</th>-->
							<th>交货期(天)</th>
						</tr>
						<tr class="add_Table">
							<td>
								<el-select v-model="addForm.itemTypeId" placeholder="" class="text_center" clearable
										   :disabled="type == 'see'">
									<el-option v-for="(item,index) in items" :key="index" :label="item"
											   :value="item"/>
								</el-select>
							</td>
							<td>
								<el-input v-model="addForm.productName" placeholder="" class="text_center" clearable
										  :disabled="type == 'see'"></el-input>
								<span @click="copyValue(addForm.productName)"  style="color: #1890ff;cursor: pointer;position: absolute;right: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
						
							</td>
							<td width="200">
								<el-input v-model="addForm.gradeValue" placeholder="" class="text_center" clearable
										  :disabled="type == 'see'"></el-input>
								<span @click="copyValue(addForm.gradeValue)"  style="color: #1890ff;cursor: pointer;position: absolute;right: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
						
							</td>
							<td>
								<el-input @input="addForm.thNumber= addForm.thNumber.replace(/[^0-9\.]/g,'')" v-model="addForm.thNumber" placeholder="" class="text_center" clearable
										  :disabled="type == 'see'" @change="totalNumChange(addForm)"></el-input>
							</td>
							<td>
								<el-input @input="addForm.peieceNum=addForm.peieceNum.replace(/[^\d]/g,'')" v-model="addForm.peieceNum" placeholder="" class="text_center" clearable
										  :disabled="type == 'see'" @change="totalNumChange(addForm)"></el-input>
							</td>
							<td>
								<el-input v-model="addForm.totalNum" placeholder="" class="text_center" clearable
										  disabled></el-input>
							</td>
<!--							<td>-->
<!--								<el-input  @input="addForm.price=addForm.price.replace(/[^0-9\.]/g,'')" v-model="addForm.price" placeholder="" class="text_center" clearable-->
<!--										   :disabled="type == 'see'" @change="totalPriceChange(addForm)"></el-input>-->
<!--							</td>-->
<!--							<td>-->
<!--								<el-input v-model="addForm.totalPrice" placeholder="" class="text_center" clearable-->
<!--										  disabled></el-input>-->
<!--							</td>-->
							<td>
								<el-input @input="addForm.goodsDay=addForm.goodsDay.replace(/[^\d]/g,'')" v-model="addForm.goodsDay" placeholder="" class="text_center" clearable
										  :disabled="type == 'see'"></el-input>
							</td>

						</tr>
					</table>
				</div>
				<el-row :gutter="40">
					<el-col :span="8">
						<el-form-item class="formList" prop="" label="备注：">
							<el-input clearable class="input_right" v-model="addForm.remark"
									  :disabled="type == 'see'"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
	</div>
</template>
<script>
	import {getStore} from "@/plugins/localStore";
	import {formatDate} from "@/plugins/formatDate";

	export default {
		props: ['editData', 'type', 'selectItems'],
		data() {
			return {
				addForm: {
					id:'',
					orderNum:'',
					itemTypeId:'',
					productName:'',
					gradeValue:'',
					thNumber:'',
					peieceNum:'',
					totalNum:'',
					price:'',
					totalPrice:'',
					goodsDay:'',
					remark:'',
					createUserId:'',
					createUserName:'',
					createTimeFt:'',
					orderType:'',
					customer:'',
					orderStatus:'',
					orderStatusCn:'',
					sureUserName:'',
					sureTime:'',
				},
				rules: {

				},
				items: [],
				count: 0,
				user: {},
			}
		},
		methods: {


			copyValue(textContent) {
				let inputDom = document.createElement('textarea'); // js创建一个文本框
				document.body.appendChild(inputDom); //将文本框暂时创建到实例里面
				inputDom.value = textContent;//将需要复制的内容赋值到创建的文本框中
				inputDom.select();//选中文本框中的内容
				inputDom.focus();
				document.execCommand('copy'); //执行复制操作
				document.body.removeChild(inputDom); //最后移出
				this.$message.success('复制成功')
			},
			totalNumChange(item){
				if(item.peieceNum && item.thNumber){
					let totalNum = parseFloat(item.peieceNum) *   parseFloat(item.thNumber);
					item.totalNum = totalNum.toFixed(4);
				}else{
					item.totalNum = '';
				}
			},

			totalPriceChange(item){
				if(item.totalNum && item.price){
					let totalPrice = parseFloat(item.price) *   parseFloat(item.totalNum) *10000;
					item.totalPrice = totalPrice.toFixed(2);
				}else{
					item.totalPrice = '';
				}
			},

			submitForm(val) {
				let self = this;
				self.$refs["addForm"].validate((valid) => {
					if (valid) {
						if (!self.addForm.customer) {
							self.$message.warning('请输入客户！');
							return;
						}
						if (!self.addForm.orderType) {
							self.$message.warning('请选择订单类型！');
							return;
						}
						self.addForm.gradeValue = self.addForm.gradeValue.trim();
						if(!self.addForm.gradeValue){
							self.$message.warning('请输入规格！');
							return;
						}
						if(!self.addForm.thNumber){
							self.$message.warning('请输入单件万支！');
							return;
						}
						let thNumber = parseFloat(self.addForm.thNumber);
						if (thNumber <= 0) {
							self.$message.warning('单件不能小于0！');
							return;
						}
						if(!self.addForm.peieceNum){
							self.$message.warning('请输入件数！');
							return;
						}
						let peieceNum = parseFloat(self.addForm.peieceNum);
						if (peieceNum <= 0) {
							self.$message.warning('件数不能小于0！');
							return;
						}
						if(!self.addForm.price){
							self.$message.warning('请输入价格！');
							return;
						}
						let price = parseFloat(self.addForm.price);
						if (price <= 0) {
							self.$message.warning('价格不能小于0！');
							return;
						}
						if(!self.addForm.goodsDay){
							self.$message.warning('请输入交货期！');
							return;
						}
						if (parseInt(self.addForm.goodsDay) <= 0) {
							self.$message.warning('交货期必须大于0！');
							return;
						}
						self.operate(val);
					} else {
						return false;
					}
				});
			},

			operate() {
				let self = this;
				let params = JSON.parse(JSON.stringify(self.addForm));
				if (self.count == 0) {
					self.$http({
						url: '/jinl/order/updateOrder',
						method: "post",
						data: JSON.stringify(params),
						dataType: 'json',
						contentType: "application/json",
					}).then(resp => {
						if (resp.success) {
							self.$notify({title: '提示', message: "操作成功！", type: 'success'});
							self.count--;
							self.$eventBus.$emit("updateOrderList");
							return;
						}
						self.$notify({title: '提示', message: resp.message, type: 'error'});
						self.count--;
					});
					self.count++;
				}
			},
		},
		watch: {
			selectItems(val) {
				this.items = val;
				console.log('---this.items----')
				console.log(this.items)
			},
			editData(val) {
				console.log(val)
				this.user = getStore();
				this.count = 0;
				for (let key in  this.addForm) {
					this.addForm[key] = val[key] || ''
				}
			},

		},
		mounted() {

		},
		components: {}
	}
</script>
<style lang="scss" scoped>
	@import "@/style/dialog.scss";

	.business-workAdd.addProject {
		.tableList {
			margin-top: 0;

			table {
				table-layout: auto;

				tr {
					background: #fff !important;

					td {
						padding: 1px 3px;position: relative;
					}

					&.noData {
						color: #999;
						height: 40px;
					}
				}

				.edit {
					color: #1890ff;
					padding: 0 5px;
					cursor: pointer;

					&.icons {
						font-size: 16px;
					}

					&:hover {
						opacity: .7;
					}
				}
			}
		}
	}
</style>